<?php
namespace Admin\Controller;
use Think\Controller;
class BandCardController  extends IndexController {
    public function lists(){
        $type=I("request.type");
        $Band=D("Bandcard_class");
        if($type==1){
            if(I("request.name")!="" && $_FILES['logo']['name']!="" && I("request.hanghao")!="") {
                $band_number = I("request.hanghao");
                if (preg_match('/^[_0-9]{12}$/i', $band_number)) {
                    $data['name'] = I("request.name");
                    $data['hanghao'] = $band_number;
                    $data['create_time'] = time();
                    if ($_FILES['logo'] != null) {
                        header("content-type:text/html;charset=utf-8");
                        $upload = new \Think\Upload(); // 实例化上传类
                        $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
                        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                        $upload->rootPath = "./Public/imageBankcard/";
                        $upload->replace = true; //存在同名文件是否是覆盖
                        $upload->autoSub = true;  // 是否使用子目录保存上传文件
                        $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
                        $info = $upload->uploadOne($_FILES['logo']);
                        if (!$info) {
                            $data['logo'] = '';
                        } else {
                            $data['logo'] = ltrim($info['savepath'] . $info['savename'], '/');
                        }
                    }
                    $result = $Band->add($data);

                    $this->redirect("BandCard/lists");
                }
            }
        }
            if($type==2){
             $this->actionUpdate();
            }
            $this->display();
    }

    public function get_list(){

        $Band=D("Bandcard_class");
        if(I("request.iDisplayStart")==''){

            $start=0;
        }else{
            $start=I("request.iDisplayStart");
        }

        $result=$Band->limit($start,I("request.iDisplayLength"))->select();
        foreach($result as $key=>&$value){
            $value['create_time']= $value['create_time']!=''? date("Y年m月d日H时i分s秒",$value['create_time']):" ";
            $value['update_time']= $value['update_time']!=''? date("Y年m月d日H时i分s秒",$value['update_time']): "";
        }
        if($result){
            $ret = array(
                "sEcho" => intval($_GET['sEcho']),
                "iTotalRecords" => $start,
                "iTotalDisplayRecords" => $Band->count(),
                "aaData" => $result
            );

                 echo json_encode($ret);exit;

        }else{
                  echo json_encode(0);
        }

    }

        public function update(){
            $id=I("request.id");
            setcookie("uid", $id, time()+3600);
            $this->display();
        }

        public function update_list(){
           $id= $_COOKIE['uid'];
           $Band=D('Bandcard_class');
           $map['id']=$id;
           $result=$Band->where($map)->field('id,name,hanghao,orders')->find();
           if($result){
               echo json_encode($result);
           }else{
               echo json_encode(0);
           }
        }

        public function actionUpdate(){
            $Band=D("Bandcard_class");
              if(I("request.name")!="" && I("request.hanghao") && I("request.orders")!="") {
                  $data['name']=I("request.name");
                  $data['hanghao'] = I("request.hanghao");
                  $data['orders'] = I("request.orders");
                  $data["update_time"] = time();
                  $id=$_COOKIE['uid'];
                  $map['id']=$id;
                  if ($_FILES['logo']['name'] != null) {
                      $logo=$Band->where($map)->getField('logo');//用于删除替换掉的照片
                      header("content-type:text/html;charset=utf-8");
                      $upload = new \Think\Upload(); // 实例化上传类
                      $upload->maxSize = 1024 * 1024;// 设置附件上传大小 (-1) 是不限值大小
                      $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                      $upload->rootPath = "./Public/imageBankcard/";
                      $upload->replace = true; //存在同名文件是否是覆盖
                      $upload->autoSub = true;  // 是否使用子目录保存上传文件
                      $upload->saveName = time() . '_' . mt_rand();   // 采用date函数生成命名规则 传入Y-m-d参数
                      $info = $upload->uploadOne($_FILES['logo']);
                      if (!$info) {
                          $data['logo'] = '';
                      } else {
                          unlink("./Public/imageBankcard/".$logo);//如果图片上传成功，删除掉旧照片
                          $data['logo'] = ltrim($info['savepath'] . $info['savename'], '/');

                      }
                  }

                  if (isset($_COOKIE["uid"])) {
                      $Band->where($map)->save($data);
                      $this->redirect("BandCard/lists");
                  }
              }
        }
        public function del(){
          $id= I("request.id");
          if($id){
             $Band= D("Bandcard_class");
              $map['id']=$id;
              $logoUrl=$Band->where($map)->getField('logo');
              if($logoUrl){
                  unlink("./Public/imageBankcard/".$logoUrl);
              }
              $result=$Band->where($map)->delete();
              if($result){
                  echo json_encode($result);
              }
          }
     }

       public function selectName(){
           $bandName=I("request.bandName");
           $Band= D("Bandcard_class");
           if($bandName!=''){
               $map['name']=$bandName;
               $result=$Band->where($map)->find();
               if($result){
                  echo json_encode(1);
              }else{
                   echo json_encode(0);
               }
           }

       }


        public function bandNumber(){
            $band_number=I("request.num");
            $Band= D("Bandcard_class");
            if($band_number!=''){
                if (!preg_match('/^[_0-9]{12}$/i', $band_number)) {
                    $note='请输入银行行号数字长度为12位';
                    echo json_encode($note);exit;
                }


                $map['hanghao']=$band_number;
                $result=$Band->where($map)->find();
                if($result){
                    $note='银行号码已存在';
                    echo json_encode($note);exit;
                }else{
                    echo json_encode(0);exit;
                }
            }

        }

}